package org.example;

import io.netty.buffer.ByteBuf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Author:qqw
 * Date:2024-06-18
 * Description:decode内容，并判断是哪个服务
 */
@Slf4j
public class ServiceSelector {

    private static List<String> serviceList;

    static {
        serviceList = new ArrayList<>(Arrays.asList(ServiceHostPortEnum.getServiceArrNonHeartBeat()));
    }

    public static String getService(Object msg){
        String string = DecodeUtil.decode((ByteBuf) msg);
        log.info("get service by msg: {}",string);
        List<String> collect = serviceList.stream().filter(string::contains).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            return null;
        }
        return collect.get(0);
    }

    public static String getService(String string){
        log.info("get service by msg: {}",string);
        List<String> collect = serviceList.stream().filter(string::contains).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(collect)){
            return null;
        }
        return collect.get(0);
    }

    public static void main(String[] args) {
        String s = "ڻ�2H2.0.203com.weihuang.ums.rms.api.service.SysDictItemSgetDictByTypeLjava/lang/String; port_codeHpath03com.weihuang.ums.rms.api.service.SysDictItemServiceremote.applicationums-omtenantId1512588980463423489deptId1512588980463423489 interface03com.weihuang.ums.rms.api.service.SysDictItemServiceversion0.0.0Z";
        String service = ServiceSelector.getService(s);
        System.out.println(service);
    }
}
